package com.huluobo.a2109monthdemo2.category

import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2109monthdemo2.R
import com.huluobo.a2109monthdemo2.base.BaseMVVMFragment
import com.huluobo.a2109monthdemo2.databinding.FragmentCategoryBinding

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding>() {
    private lateinit var viewModel: CategoryViewModel

    private lateinit var parentAdapter: CategoryParentAdapter

    private val categoryParentList = mutableListOf<CategoryBean>()

    private lateinit var childAdapter: CategoryChildAdapter

    private val categoryChildList = mutableListOf<CategoryBean>()
    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[CategoryViewModel::class.java]
        viewModel.getCategory()
        viewModel.success.observe(this) {
            if (it.code == 200) {
                //一级列表初始化
                categoryParentList.clear()
                categoryParentList.addAll(it.data)
                parentAdapter.notifyDataSetChanged()

                //二级列表初始化
                val data = categoryParentList[0].data
                if (data != null) {
                    categoryChildList.clear()
                    categoryChildList.addAll(data)
                    childAdapter.notifyDataSetChanged()
                }
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.failed.observe(this) {
            Log.i("TagA", "获取一二级列表失败:$it")
        }

        viewModel.getGoodsListSuccess.observe(this) {
            Log.i("TagA", "三级列表:${it.data.size}")
        }

        viewModel.getGoodsListFailed.observe(this) {
            Log.i("TagA", "获取三级列表失败:$it")
        }
    }

    override fun initView() {
        //初始化一级分类列表
        dataBinding.categoryRvParent.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(categoryParentList)
        dataBinding.categoryRvParent.adapter = parentAdapter

        //一级列表点击事件,切换二级的数据源
        parentAdapter.setOnItemClickListener { adapter, view, position ->
            parentAdapter.setSelectItemId(categoryParentList[position].id)
            val data = categoryParentList[position].data
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()
            }
        }

        //初始化二级列表
        dataBinding.categoryRvChild.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(categoryChildList)
        dataBinding.categoryRvChild.adapter = childAdapter

        //二级列表的点击事件
        childAdapter.setOnItemClickListener { adapter, view, position ->
            if (categoryChildList.size != 0) {
                viewModel.getCategoryGoodsList(categoryChildList[position].id, 1, 10)
            }
        }
    }
}